package com.heima.article.mq;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.model.wemedia.pojos.pojos.WmNews;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author mgw
 * @create 2021/10/6 17:38
 */
@Component
public class SyncArticle {
    @Autowired
    private ApArticleService apArticleService;
    /**
     * 监听文章内容审核结果
     */
    @KafkaListener(topics = "verifyArticle",groupId = "articleSynGrop")
    public void articleVerifyResultBack(ConsumerRecord<?,?> record){
        String value = record.value().toString();
        Map map = JSON.parseObject(value, Map.class);
        System.out.println(map);
        List<Map> mapList = JSON.parseArray(map.get("verifyResult").toString(), Map.class);
        for (Map map1 : mapList) {
            String suggestion = map.get("suggestion").toString();
            if (suggestion.equals("view")||suggestion.equals("block")){
                return;
            }
        }
        String attach = map.get("attach").toString();
        WmNews news = JSON.parseObject(attach, WmNews.class);
        apArticleService.syncArticle(news);
    }
    @KafkaListener(topics = "syncarticle",groupId = "syncarticlegroup")
    public void listenerSyncArticle(ConsumerRecord<?,?> record){
        String resultJson = record.value().toString();
        Map map = JSON.parseObject(resultJson, Map.class);
        apArticleService.downOrUp(map);

    }
}
